package com.ruoyi.gas.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import com.ruoyi.gas.mapper.ExtSaleRecordMapper;
import com.ruoyi.gas.domain.ExtSaleRecord;
import com.ruoyi.gas.service.IExtSaleRecordService;

/**
 * 扩展销售记录Service业务层处理
 * 
 * @author morris
 * @date 2023-12-08
 */
@Service
public class ExtSaleRecordServiceImpl implements IExtSaleRecordService 
{
    @Autowired
    private ExtSaleRecordMapper extSaleRecordMapper;

    /**
     * 查询扩展销售记录
     * 
     * @param saleRecordId 扩展销售记录主键
     * @return 扩展销售记录
     */
    @Override
    public ExtSaleRecord selectExtSaleRecordBySaleRecordId(Long saleRecordId)
    {
        return extSaleRecordMapper.selectExtSaleRecordBySaleRecordId(saleRecordId);
    }

    /**
     * 查询扩展销售记录列表
     * 
     * @param extSaleRecord 扩展销售记录
     * @return 扩展销售记录
     */
    @Override
    public List<ExtSaleRecord> selectExtSaleRecordList(ExtSaleRecord extSaleRecord)
    {
        return extSaleRecordMapper.selectExtSaleRecordList(extSaleRecord);
    }

    /**
     * 新增扩展销售记录
     * 
     * @param extSaleRecord 扩展销售记录
     * @return 结果
     */
    @Override
    public int insertExtSaleRecord(ExtSaleRecord extSaleRecord)
    {
        extSaleRecord.setCreateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null){
            extSaleRecord.setCreateBy(loginUser.getUsername());
        }

        return extSaleRecordMapper.insertExtSaleRecord(extSaleRecord);
    }

    /**
     * 修改扩展销售记录
     * 
     * @param extSaleRecord 扩展销售记录
     * @return 结果
     */
    @Override
    public int updateExtSaleRecord(ExtSaleRecord extSaleRecord)
    {
        extSaleRecord.setUpdateTime(DateUtils.getNowDate());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser != null){
            extSaleRecord.setUpdateBy(loginUser.getUsername());
        }
        return extSaleRecordMapper.updateExtSaleRecord(extSaleRecord);
    }

    /**
     * 批量删除扩展销售记录
     * 
     * @param saleRecordIds 需要删除的扩展销售记录主键
     * @return 结果
     */
    @Override
    public int deleteExtSaleRecordBySaleRecordIds(Long[] saleRecordIds)
    {
        return extSaleRecordMapper.deleteExtSaleRecordBySaleRecordIds(saleRecordIds);
    }

    /**
     * 删除扩展销售记录信息
     * 
     * @param saleRecordId 扩展销售记录主键
     * @return 结果
     */
    @Override
    public int deleteExtSaleRecordBySaleRecordId(Long saleRecordId)
    {
        return extSaleRecordMapper.deleteExtSaleRecordBySaleRecordId(saleRecordId);
    }

    /**
     * 订单完成
     *
     * @param saleRecordId 扩展销售记录主键
     * @return 结果
     */
    @Override
    public int saleFinishBySaleRecordId(Long saleRecordId)
    {
        ExtSaleRecord extSaleRecord = extSaleRecordMapper.selectExtSaleRecordBySaleRecordId(saleRecordId);
        if (extSaleRecord == null){
            return -1;
        }
        extSaleRecord.setUpdateTime(DateUtils.getNowDate());
        extSaleRecord.setFinishTime(DateUtils.getNowDate());
        extSaleRecord.setSaleState(1);
        extSaleRecord.setColorAlertProc(2);
        extSaleRecord.setVoiceAlertProc(2);
        return extSaleRecordMapper.updateExtSaleRecord(extSaleRecord);
    }

    /**
     * 处理声音告警
     *
     * @param saleRecordId 扩展销售记录主键
     * @return 结果
     */
    @Override
    public int voiceAlertBySaleRecordId(Long saleRecordId)
    {
        ExtSaleRecord extSaleRecord = extSaleRecordMapper.selectExtSaleRecordBySaleRecordId(saleRecordId);
        if (extSaleRecord == null){
            return -1;
        }
        extSaleRecord.setVoiceAlertProc(2);
        return extSaleRecordMapper.updateExtSaleRecord(extSaleRecord);
    }

    /**
     * 处理颜色告警
     *
     * @param saleRecordId 扩展销售记录主键
     * @return 结果
     */
    @Override
    public int colorAlertBySaleRecordId(Long saleRecordId)
    {
        ExtSaleRecord extSaleRecord = extSaleRecordMapper.selectExtSaleRecordBySaleRecordId(saleRecordId);
        if (extSaleRecord == null){
            return -1;
        }
        extSaleRecord.setColorAlertProc(2);
        return extSaleRecordMapper.updateExtSaleRecord(extSaleRecord);
    }

}
